package org.gd.science.kttsoft;

/**
 * @author Gorhover D. P.
 */
public class CharArrayUtils {

    /**
     * <pre><code>
     *
     *  int newLength = endIndex-startIndex;
     *  if (newLength < 0)
     *      throw new IllegalArgumentException(startIndex + " > " + endIndex);
     *  char[] chars1 = new char[endIndex-startIndex];
     *  for(int i=startIndex, j=0; i<endIndex && j<chars1.length; i++, j++)
     *      chars1[j] = chars[i];
     *  return chars1;
     *
     * </code></pre>
     *
     * java.util.Arrays#copyOfRange is faster
     * @see java.util.Arrays#copyOfRange(char[] original, int from, int to)
     */
    public static char[] subList(char[] chars, int startIndex, int endIndex) {
        return java.util.Arrays.copyOfRange(chars, startIndex, endIndex);
    }

    public static boolean contains(char[] chars, char c) {
        return indexOf(chars, c) > -1;
    }

    public static int indexOf(char[] chars, char c) {
        for(int i=0; i<chars.length; i++) {
            if(c == chars[i]) {
                return i;
            }
        }
        return -1;
    }
}
